package org.amphiprion.diceroller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.amphiprion.diceroller.util.PreferenceUtil;

import android.app.Application;
import android.util.Log;

public class DiceRollerApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();

		PreferenceUtil.init(this);
		boolean forceCopy = PreferenceUtil.isFirstRunForThisVersion();
		forceCopy = true;
		// Log.d(ApplicationConstants.PACKAGE, "111111111111111111111111 " + forceCopy);
		File meshDir = new File(ApplicationConstants.MESH_DIRECTORY);
		copyJavaResourceToSdCard("/resources/d4.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d4.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d4-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d4-numberNormalLink.xml", meshDir, forceCopy);

		copyJavaResourceToSdCard("/resources/d6.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d6.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d6-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d6-numberNormalLink.xml", meshDir, forceCopy);

		copyJavaResourceToSdCard("/resources/d8.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d8.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d8-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d8-numberNormalLink.xml", meshDir, forceCopy);

		copyJavaResourceToSdCard("/resources/d10.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d10.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d10-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d10-numberNormalLink.xml", meshDir, forceCopy);

		copyJavaResourceToSdCard("/resources/d12.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d12.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d12-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d12-numberNormalLink.xml", meshDir, forceCopy);

		copyJavaResourceToSdCard("/resources/d20.obj", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d20.mtl", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d20-layout.png", meshDir, forceCopy);
		copyJavaResourceToSdCard("/resources/d20-numberNormalLink.xml", meshDir, forceCopy);

		// DiceDefinitionDao.getInstance().getDiceDefinition("d6");

		// Mice & Mystics.
		File miceDir = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/Mice & Mystics/");
		copyJavaResourceToSdCard("/resources/games/micemystic/action-view.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/action.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/attack-view.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/attack.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/background.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/cheese.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/defense-view.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/defense.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/game-definition.xml", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/melee.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/move.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/range.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/shield.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/star.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/viewer_background.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/viewer_left.png", miceDir);
		copyJavaResourceToSdCard("/resources/games/micemystic/viewer_right.png", miceDir);

		// Temporaire
		// Descent.
		File descentDir = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/DescentV2/");
		copyJavaResourceToSdCard("/resources/games/descent/black-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/black.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/blue-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/blue.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/brown-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/brown.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/damage.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/fail.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/game-definition.xml", descentDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/descent/range.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/red-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/red.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/shield.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/surge.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/white-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/white.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/yellow-view.png", descentDir);
		copyJavaResourceToSdCard("/resources/games/descent/yellow.png", descentDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/descent/background.png", descentDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/descent/viewer_background.png", descentDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/descent/viewer_left.png", descentDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/descent/viewer_right.png", descentDir, forceCopy);

		// Classic.
		File classicDir = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/Classic/");
		copyJavaResourceToSdCard("/resources/games/classic/d10-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d10.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d12-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d12.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d20-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d20.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d4-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d4.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d6-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d6.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d8-view.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d8.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/game-definition.xml", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/range.png", classicDir);
		copyJavaResourceToSdCard("/resources/games/classic/d100x00-view.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d100x00.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d100x0-view.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/d100x0.png", classicDir, forceCopy);
		copyJavaResourceToSdCard("/resources/games/classic/percent.png", classicDir);

		// Catane
		File cataneDir = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/Catane/");
		copyJavaResourceToSdCard("/resources/games/catane/background.png", cataneDir);
		copyJavaResourceToSdCard("/resources/games/catane/d6-view.png", cataneDir);
		copyJavaResourceToSdCard("/resources/games/catane/d6.png", cataneDir);
		copyJavaResourceToSdCard("/resources/games/catane/diceChooserBackgroundImage.png", cataneDir);
		copyJavaResourceToSdCard("/resources/games/catane/game-definition.xml", cataneDir);
		copyJavaResourceToSdCard("/resources/games/catane/range.png", cataneDir);

		// Zombicide
		File zombicideDir = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/Zombicide/");
		copyJavaResourceToSdCard("/resources/games/zombicide/background.png", zombicideDir);
		copyJavaResourceToSdCard("/resources/games/zombicide/d6-view.png", zombicideDir);
		copyJavaResourceToSdCard("/resources/games/zombicide/d6.png", zombicideDir);
		copyJavaResourceToSdCard("/resources/games/zombicide/chooser.png", zombicideDir);
		copyJavaResourceToSdCard("/resources/games/zombicide/game-definition.xml", zombicideDir);
		copyJavaResourceToSdCard("/resources/games/zombicide/range.png", zombicideDir);

	}

	private void copyJavaResourceToSdCard(String resource, File directory) {
		copyJavaResourceToSdCard(resource, directory, false);
	}

	private void copyJavaResourceToSdCard(String resource, File directory, boolean forceCopy) {
		directory.mkdirs();

		byte[] b = new byte[2048];
		int len = 0;

		String filename = resource;
		int last = resource.lastIndexOf("/");
		if (last != -1) {
			filename = resource.substring(last + 1);
		}
		File f = new File(directory, filename);
		if (forceCopy || !f.exists()) {
			// Log.d(ApplicationConstants.PACKAGE, "copying " + filename + " to " + directory.getAbsolutePath());
			try {
				FileOutputStream fos = new FileOutputStream(f);
				InputStream is = getClass().getResourceAsStream(resource);
				while ((len = is.read(b)) > 0) {
					fos.write(b, 0, len);
				}
				fos.close();
				is.close();
			} catch (IOException e) {
				Log.e(ApplicationConstants.PACKAGE, "", e);
			}
		}

	}
}
